package org.etlt;

public class JavaStyleIdentifier {
    /**
     * inclusive
     */
    private final int start;
    /**
     * exclusive
     */
    private final int end;
    /**
     *
     */
    private final String name;

    public JavaStyleIdentifier(int start, int end, String name) {
        this.start = start;
        this.end = end;
        this.name = name;
    }

    public int getStart() {
        return start;
    }

    public int getEnd() {
        return end;
    }

    public String getName() {
        return name;
    }

    /**
     *
     * @param text
     * @param start exclusive
     * @return
     */
    public static JavaStyleIdentifier findFollowingIdentifier(String text, int start){
        if(text.length() <= start + 1)
            return null;
        int index = start + 1;
        char ch = text.charAt(index);
        if(!Character.isJavaIdentifierStart(ch))
            return null;
        StringBuilder stringBuilder = new StringBuilder(text.length());
        stringBuilder.append(ch);
        for(index++;index < text.length();index++){
            ch = text.charAt(index);
            if(Character.isJavaIdentifierPart(ch)){
                stringBuilder.append(ch);
            }else {
                break;
            }
        }
        if(index == text.length()){
            index--;
        }
        JavaStyleIdentifier identifier = new JavaStyleIdentifier(start + 1, start + 1 + stringBuilder.length(), stringBuilder.toString());
        return identifier;
    }
}
